﻿using System;
using System.Collections.Generic;

namespace SGH
{
    class Gen
    {
        
        public int patternNum;
        public int sheetNum;
        public int offcutNum;//qq
        public double offcutSquer;//qq
        public Global global;
        public List<List<int[]>> pattern;

        /// <summary>
        /// 余料种类数
        /// </summary>
        public int OffcutTypeNum {
            get
            {
                int num = 0;
                foreach (var item in OffcutType)
                {
                    num += item;
                }
                return num;
            }
        }

        /// <summary>
        /// 记录余料种类，一维毛坯种类，二维行数
        /// </summary>
        public static int[,] OffcutType { get; private set; }

        public Gen(int delta,double beta1)
        {
            global = new Global();
            global.theta = delta;
            global.beta = beta1;
            int n=Constant.blankTypeNum;
            pattern = new List<List<int[]>>();
            global.restDemand = new List<int[]>();
            global.blankValue = new double[n];
            global.CISET = new List<int[]>();
            global.stripe = new Stripe();
            for (int i = 0; i < n; i++)
            {
                int[] a = new int[2];
                a[0] = i;
                a[1] = Constant.demand[i];
                global.restDemand.Add(a);
                global.blankValue[i] =Constant.PI*Constant.diameter[i] * Constant.diameter[i]/4;
            }
            OffcutType= new Stripe().OffcutWidth;
            Array.Clear(OffcutType,0, OffcutType.Length);
        }
        public void gen()
        {
            sheetNum = 0;
            patternNum = 0;
            while (global.restDemand.Count > 0)
            {
                
                int num=doPattern();
                if(num!=0)
                {
                    patternNum = patternNum + 1;
                }
                if (num==0)
                {
                    break;
                }
                sheetNum += num;
                if (Constant.IsUseControl&&Constant.IsControl&&OffcutTypeNum > Constant.OffcutTypeNumLimit)
                {
                    Constant.IsUseControl = false;
                }
                
            }
        }
        private int  doPattern()
        {
            Select.select(global);
            Pattern pat = new Pattern(global);
            pat.DoPattern();
            int frequency = Correct.updateBlankInfo(pat.p, global);
            int[] num = new int[1] { frequency };
            pat.layout.Add(num);
            pattern.Add(pat.layout);
            //如果用上了余料就记录起来
            if (pat.IsOffcut)
            {
                /*
            * 毛坯id：   s[0] 
            * 毛坯排数:  s[1] 
            * 条带方向： s[2] 
            * 条带长度： s[3] 
            * 毛坯个数： s[4]
            */
                //取余料条带
                var s = pat.layout[0];
                OffcutType[s[0],s[1]]++;
                offcutNum += frequency;//qq
                var dia = Constant.diameter[s[0]];//qq
                var row = s[1] + 1;//qq
                var stripeBlankNum = global.stripe.OffcutStripe[s[0], Constant.sheetWidth, s[1]];
                offcutSquer += Math.PI*Math.Pow(dia/2,2) * stripeBlankNum * frequency;//qq.在使用余料的情况下，计算余料的毛坯面积
            }
            return frequency;
        }

        
    }
}
